//
//  SSVisitorView.swift
//  MyMicroBlog
//
//  Created by stevenSu on 15/7/29.
//  Copyright © 2015年 stevenSu. All rights reserved.
//

import UIKit
private let SSVisitorViewBackgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
private let hintLabelFontSize: CGFloat = 14
protocol SSVisitorViewDelegate: NSObjectProtocol {
    func visitorViewWillRegister()
    func visitorViewWillLogin()
}

class SSVisitorView: UIView {
    private let rotateViewAnimation = 20.0
    private let rotateViewCenterYMargin: CGFloat = -40
    private let hintLabelYMargin: CGFloat = 35
    private let hintLabelWidth: CGFloat = 240
    private let buttonYMargin: CGFloat = 55
    private let buttonWidth: CGFloat = 100
    private let buttonHeight: CGFloat = 40

    weak var delegate: SSVisitorViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        backgroundColor = SSVisitorViewBackgroundColor
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    
    /// 设置UI内容
    ///
    /// - parameter iconViewImage   : 图标名称
    /// - parameter hintLabelContent: 提示内容
    /// - parameter isHome          : 是否Home页
    func setupUIContent(iconViewImage: String, hintLabelContent: String, isHome: Bool) {
        if isHome {
            startAnimation()
        } else {
            rotateView.hidden = true
        }
        iconView.image = UIImage(named: iconViewImage)
        hintLabel.text = hintLabelContent
        
    }
    
    /// 旋转rotateView
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = rotateViewAnimation
        animation.removedOnCompletion = false
        rotateView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    /// 设置UI布局
    private func setupUI() {
        addSubview(rotateView)
        addSubview(homeMaskView)
        addSubview(iconView)
        addSubview(hintLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        rotateView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: rotateView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: rotateView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: rotateViewCenterYMargin))
        
        homeMaskView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": homeMaskView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-[button]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": homeMaskView, "button": registerButton]))
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: rotateView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: rotateView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        hintLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: hintLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: rotateView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: hintLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: rotateView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: hintLabelYMargin))
        addConstraint(NSLayoutConstraint(item: hintLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: hintLabelWidth))
        
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: hintLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: hintLabel, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: buttonYMargin))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: buttonWidth))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: buttonHeight))
        
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: hintLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: hintLabel, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: buttonYMargin))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: buttonWidth))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: buttonHeight))
    }
    
    func registerClicked() {
        delegate?.visitorViewWillRegister()
    }
    
    func loginClicked() {
        delegate?.visitorViewWillLogin()
    }
    
    private lazy var rotateView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var homeMaskView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var hintLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(hintLabelFontSize)
        return label
        }()
    
    private lazy var registerButton: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "registerClicked", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "loginClicked", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
}
